<template>
  <div class="">
    <el-row justify="end" class="title-add">
      <el-col :span='4' :offset=20 align=right>
        <el-button type="primary" @click="edit(null)">添加</el-button>
      </el-col>
    </el-row>
    <el-table
      :data="questionsList"
      stripe
      border
      fit
      style="width: 100%;">
      <el-table-column
        prop="id"
        align="center"
        width="80"
        label="序号"
      >
      </el-table-column>
      <el-table-column
        prop="title"
        align="center"
        label="题目标题"
      >
      </el-table-column>
      <el-table-column
        prop="content"
        align="center"
        label="内容"
      >
      </el-table-column>
      <el-table-column
        align="center"
        width="200"
        label="操作">
        <template slot-scope="scope">
          <el-button @click="del(scope.row)" type="danger" size="small">删除</el-button>
          <el-button @click="edit(scope.row)" type="primary" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="编辑/添加 用户" :visible.sync="dialogVisible" width="30%">
      <el-form label-width="80px" label-position="right">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="form.content"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'questiongList',
  data(){
    return{
      questionsList:[],
      align: 'center',
      dialogVisible: false,
      form: {
        title: '',
        content: ''
      }
    }
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList(){
      let loading = this.$loading();
      let resArr = await this.$axios({
        url: '/questions/list'
      });
      this.questionsList = resArr.data.data;
      loading.close();
    },
    async edit(question){
      this.dialogVisible = true;
      this.form.title = !!question ? question.title : '';
      this.form.content = !!question ? question.content : '';
      this.form.id = !!question ? question.id : '';
    },
    async del(user, index) {
      await this.$axios({
        url: "/questions/del",
        method: "get",
        params: {
          id: user.id
        }
      });
      this.questionsList.splice(index, 1);
    },
    async submitEdit() {
      let loading = this.$loading();
      let resObj = await this.$axios({
        url: "/questions/add",
        method: "post",
        data: this.form
      });
      loading.close();
      if (resObj.data.code == '1') {
        this.$message({
          message: resObj.data.msg,
          type: "error"
        });
      } else {
        this.dialogVisible = false;
        this.$message({
          message: "保存成功",
          type: "success"
        });
      }
      this.getList();
    }
  }
}
</script>
<style scoped>
.title-add{
  padding-bottom: 10px;
}
</style>


